using AutoMapper;
using DealerPlatform.Domain;
using DealerPlatform.Service.CustomerApp.Dtos;
using DealerPlatform.Service.OrderApp.Dtos;
using DealerPlatform.Service.ProductApp.Dtos;
using DealerPlatform.Service.ShoppingCartApp.Dtos;
using Newtonsoft.Json;

namespace DealerPlatform.Service;

public class DealerPlatformAutoMapperProfile : Profile
{
    public DealerPlatformAutoMapperProfile()
    {
        CreateMap<Product, ProductDto>().ReverseMap();
        CreateMap<Customer, CustomerDto>().ReverseMap();
        CreateMap<ProductSale, ProductDto>().ReverseMap();
        CreateMap<ProductSaleAreaDiff, ProductDto>().ReverseMap();
        CreateMap<ProductPhoto, ProductDto>().ReverseMap();
        CreateMap<ShoppingCart, ShoppingCartInputDto>().ReverseMap();
        CreateMap<ShoppingCart, ShoppingCartDto>().ReverseMap();
        CreateMap<CustomerInvoice, InvoiceOfOrderConfirmDto>().ReverseMap();
        CreateMap<SaleOrderMaster, SaleOrderDto>().ReverseMap();
        CreateMap<ProductDto, ProductCto>()
            .ForMember(cto => cto.ProductPhoto,
                m => m.MapFrom(dto =>
                    JsonConvert.SerializeObject(dto.ProductPhoto)))
            .ForMember(cto => cto.ProductSale,
                m => m.MapFrom(dto =>
                    JsonConvert.SerializeObject(dto.ProductSale)));
        CreateMap<ProductCto, ProductDto>()
            .ForMember(dto => dto.ProductPhoto,
                m => m.MapFrom(cto =>
                    JsonConvert.DeserializeObject<ProductPhoto>(cto.ProductPhoto)))
            .ForMember(dto => dto.ProductSale,
                m => m.MapFrom(cto =>
                    JsonConvert.DeserializeObject<ProductSale>(cto.ProductSale)));
    }
}